package org.jvending.provisioning.deliverable;

//import org.jvending.provisioning.descriptor.maven.MavenDescriptorType;

import javax.provisioning.Deliverable;
import java.net.URI;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

public class MavenDescriptorFile extends BaseDescriptorFile {

    private static Logger logger = Logger.getLogger("MavenDescriptorFile");

    private final URL url;

    private final URI uri;

    private final String mimeType;

    private Map<String, String> properties = new HashMap<String, String>();

    public MavenDescriptorFile(URL url, URI uri, String mimeType, List<Deliverable> contentFiles) {
        super(url, uri, mimeType, contentFiles);
        this.url = url;
        this.uri = uri;
        this.mimeType = mimeType;
        loadProperties();
    }

    public URI getURI() {
        return uri;
    }

    public String getAppProperty(String name) {
        return properties.get(name);
    }

    public void setAppProperty(String name, String value) {
        properties.put(name, value);
    }

    private void loadProperties() {
        /*
        InputStream is = null;
        MavenDescriptorType mavenDescriptorType = null;
        try {
            is = getURL().openStream();
            if (is == null) throw new IOException("JV-1251-004: URL Value is null");
            try {
                JAXBContext jaxbContext = JAXBContext.newInstance("org.jvending.provisioning.descriptor.maven");
                Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
                unmarshaller.setValidating(true);
                mavenDescriptorType = (MavenDescriptorType) unmarshaller.unmarshal(is);
            } catch (JAXBException e) {
                e.printStackTrace();
                return;
            }

        } catch (IOException e) {
            logger.log(Level.INFO, "JV-1251-005:Could not load properties: URL = " + url +
                    ", Mime-Type = " + mimeType, e);
            return;
        } finally {
            try {
                if (is != null) is.close();
            } catch (IOException e) {
                logger.info("JV-1251-006: Could not close connection.");
            }
        }

        properties.put("contentUri", mavenDescriptorType.getContentFile());
        properties.put("projectUri", mavenDescriptorType.getProjectFile());
        */
    }
}
